package top.shiyiri.exercise;

import org.junit.Test;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @author Aunean
 * @date 2022/1/5
 */
/*
创建该类的 5 个对象，并把这些对象放入 TreeSet 集合中（下一章：
TreeSet 需使用泛型来定义）
分别按以下两种方式对集合中的元素进行排序，并遍历输出：
1). 使 Employee 实现 Comparable 接口，并按 name 排序
2). 创建 TreeSet 时传入 Comparator 对象，按生日日期的先后排序
 */
public class EmployeeTest {
    TreeSet<Employee> set = new TreeSet<>();

    Employee e1 = new Employee("Tom", 34, new MyDate(2020, 2, 22));
    Employee e2 = new Employee("Jerry", 34, new MyDate(2021, 11, 11));
    Employee e3 = new Employee("Anna", 34, new MyDate(2010, 12, 12));
    Employee e4 = new Employee("Tomas", 34, new MyDate(2010, 2, 24));
    Employee e5 = new Employee("Queen", 34, new MyDate(1997, 10, 9));
    {
        set.add(e1);
        set.add(e2);
        set.add(e3);
        set.add(e4);
        set.add(e5);
    }

    @Test
    public void test1() {
        //使 Employee 实现 Comparable 接口，并按 name 排序
        for (Employee employee: set) {
            System.out.println(employee);
        }
    }

    @Test
    public void test2() {
        //创建 TreeSet 时传入 Comparator 对象，按生日日期的先后排序
        Comparator<Employee> comparator = new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                MyDate e1 = o1.getBirthday();
                MyDate e2 = o2.getBirthday();
                int minYear = e1.getYear() - e2.getYear();
                if (minYear != 0) {
                    return minYear;
                }

                int minMonth = e1.getMonth() - e2.getMonth();
                if (minMonth != 0) {
                    return minMonth;
                }

                return e1.getDay() - e2.getDay();
            }
        };

        TreeSet<Employee> set2 = new TreeSet<>(comparator);
        set2.add(e1);
        set2.add(e2);
        set2.add(e3);
        set2.add(e4);
        set2.add(e5);
        for (Employee employee: set2) {
            System.out.println(employee);
        }

    }
}
